package com.growthserver.service.impl;

import com.github.pagehelper.PageHelper;
import com.growthserver.entity.TMemberDiscountEntity;
import com.growthserver.entity.TMemberEntity;
import com.growthserver.entity.TMemberEquityEntity;
import com.growthserver.entity.TMemberLevelEntity;
import com.growthserver.mapper.EquityCenterMapper;
import com.growthserver.param.CommonParam;
import com.growthserver.param.TMemberDiscountParam;
import com.growthserver.param.TMemberEquityParam;
import com.growthserver.param.TMemberLevelParam;
import com.growthserver.service.EquityCenterService;
import com.growthserver.vo.TMemberDiscountVo;
import com.growthserver.vo.TMemberEquityVo;
import com.growthserver.vo.TMemberLevelVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class EquityCenterServiceImpl implements EquityCenterService {

    private final Logger LOGGER = LoggerFactory.getLogger(EquityCenterService.class);

    @Autowired
    private EquityCenterMapper equityCenterMapper;


    /**
     * 根据类型展示,会员管理列表
     * @param tMemberLevelParam
     * @return
     */
    @Override
    public List<TMemberLevelEntity> listDisplay(TMemberLevelParam tMemberLevelParam) {
        PageHelper.startPage(tMemberLevelParam.getPageNo(), tMemberLevelParam.getPageSize());
        return equityCenterMapper.memberListDisplay(tMemberLevelParam);
        }

    /**
     * 权益列表展示
     * @param tMemberEquityParam
     * @return
     */
    @Override
    public List<TMemberEquityEntity> equityListDisplay(TMemberEquityParam tMemberEquityParam) {
        PageHelper.startPage(tMemberEquityParam.getPageNo(), tMemberEquityParam.getPageSize());
        return equityCenterMapper.equityListDisplay(tMemberEquityParam);
    }

    /**
     * 权益折扣列表
     * @param tMemberDiscountParam
     * @return
     */
    @Override
    public List<TMemberDiscountEntity> discountListDisplay(TMemberDiscountParam tMemberDiscountParam) {
        PageHelper.startPage(tMemberDiscountParam.getPageNo(), tMemberDiscountParam.getPageSize());
        return equityCenterMapper.discountListDisplay(tMemberDiscountParam);
    }

    @Override
    public TMemberLevelEntity getUserLeveL(String growthValue,String userType) {
        return equityCenterMapper.getUserLeveL(growthValue,userType);
    }

    /**
     * 编辑保存 会员管理信息
     * @param tMemberLevelEntity
     */
    @Override
    public void updateMember(TMemberLevelEntity tMemberLevelEntity) {
        equityCenterMapper.memberUpdate(tMemberLevelEntity);

    }

    /**
     * 编辑保存 会员权益信息
     * @param tMemberEquityEntity
     */
    @Override
    public void updateEquity(TMemberEquityEntity tMemberEquityEntity) {
        equityCenterMapper.equityUpdate(tMemberEquityEntity);
    }

    /**
     * 编辑保存 权益折扣信息
     * @param tMemberDiscountEntity
     */
    @Override
    public void updateDiscount(TMemberDiscountEntity tMemberDiscountEntity) {
        equityCenterMapper.discountUpdate(tMemberDiscountEntity);

    }

    /**
     * 根据type,ID是否启用
     * @param commonParam
     */

    @Override
    public void deleteMember(CommonParam commonParam) {
        String type = commonParam.getType();
        switch (type){
            //会员等级展示
            case "1":
                equityCenterMapper.memberDelete(commonParam.getId());
                break;
            //会员权益展示
            case "2":
                equityCenterMapper.equityDelete(commonParam.getId());
                break;
            //会员折扣展示
            case "3":
                equityCenterMapper.discountDelete(commonParam.getId());
                break;
        }
    }

    /**
     * 根据type,ID删除
     * @param commonParam
     */

    @Override
    public void isEnable(CommonParam commonParam) {
        String type = commonParam.getType();
        switch (type){
            //会员等级展示
            case "1":
                equityCenterMapper.memberIsEnable(commonParam.getId(),commonParam.getIsEnable());
                break;
            //会员权益展示
            case "2":
                equityCenterMapper.equityIsEnable(commonParam.getId(),commonParam.getIsEnable());
                break;
            //会员折扣展示
            case "3":
                equityCenterMapper.discountIsEnable(commonParam.getId(),commonParam.getIsEnable());
                break;
        }
    }

    /**
     * 创建会员等级信息
     * @param tMemberLevelVo
     */
    @Override
    public void createMemberLvl(TMemberLevelVo tMemberLevelVo) {
        //查询是否存在此种类型
        equityCenterMapper.createMemberLvl(tMemberLevelVo);
    }

    /**
     * 会员等级校验
     * @param tMemberLevelVo
     */
    @Override
    public List<TMemberLevelEntity> memberLvlCheck(TMemberLevelVo tMemberLevelVo) {
        //查询是否存在此种类型
       return equityCenterMapper.memberLvlCheck(tMemberLevelVo);
    }

    /**
     * 创建会员权益数据
     * @param tMemberEquityVo
     */
    @Override
    public void createMemberEquity(TMemberEquityVo tMemberEquityVo) {
        equityCenterMapper.createMemberEquity(tMemberEquityVo);
    }

    /**
     * 会员权益校验
     * @param tMemberEquityVo
     */
    @Override
    public HashMap<String, Object> memberEquityCheck(TMemberEquityVo tMemberEquityVo) {
        HashMap<String, Object> map = new HashMap<>();
        //获取当前用户成长值区间
        HashMap<String, String> intervalMap = equityCenterMapper.memberLevelByUserTypeAndLevelNum(tMemberEquityVo.getLevelNum());
        map.put("intervalMap",intervalMap);
        //
        List<String> hashMapList = equityCenterMapper.memberEquityCheck(tMemberEquityVo.getLevelNum());
        List<Object> arrayList = new ArrayList<>();
        hashMapList.forEach(p->{
            HashMap<String, String> hashMap = new HashMap<>();
            String[] split = p.split(",");
            hashMap.put("equityId",split[0]);
            hashMap.put("min",split[1]);
            hashMap.put("max",split[2]);
            arrayList.add(hashMap);
        });
        map.put("hashMapList",arrayList);
        return map;
    }

    /**
     * 创建权益折扣信息
     * @param tMemberDiscountVo
     */
    @Override
    public void createMemberDiscount(TMemberDiscountVo tMemberDiscountVo) {
        equityCenterMapper.createMemberDiscount(tMemberDiscountVo);
    }

    /**
     * 权益折扣枚举
     * @param commonParam
     * @return
     */
    @Override
    public List<HashMap<String,String>> equityLvlEnum(CommonParam commonParam) {
        return equityCenterMapper.equityLvlEnum(commonParam);
    }

    /**
     * 权益创建等级枚举
     * @param commonParam
     * @return
     */
    @Override
    public List<HashMap<String, String>> lvlEnum(CommonParam commonParam) {
        return equityCenterMapper.lvlEnum(commonParam);
    }

    @Override
    public void updateMemberInfo(TMemberEntity tMemberEntity) {
         equityCenterMapper.updateMemberInfo(tMemberEntity);
    }

}
